%将通过filterDesigner生成的SOS、G矩阵处理，保存到本地的coeffs_B.txt、coeffs_A.txt和scaleFactors.txt
 
SOS_num=size(SOS,1);%获取SOS矩阵的行数
G_num=size(G,1);%获取G矩阵的行数
%对生成的系数A取反
SOS(:,5)=-SOS(:,5);
SOS(:,6)=-SOS(:,6);
 
%将滤波器设计器生成的滤波器系数打印到coeffs_B,coeffs_A.txt文件中
fileID = fopen('iir_coeffs.h','w');%写文件
fprintf(fileID,"#ifndef __IIR_COEFFS_H_\n");
fprintf(fileID,"#define __IIR_COEFFS_H_\n\n\n");

fprintf(fileID,"#define SOS_NUM (25U)\n\n");

fprintf(fileID,"const float coeffs_B[SOS_NUM*3] = {\n");

for i=1:1:SOS_num
    fprintf(fileID,'    %6f,%6f,%6f,\n',SOS(i,1),SOS(i,2),SOS(i,3));
end

fprintf(fileID,"};\n\n");

fprintf(fileID,"const float coeffs_A[SOS_NUM*3] = {\n");

for i=1:1:SOS_num
    fprintf(fileID,'    %6f,%6f,%6f,\n',SOS(i,4),SOS(i,5),SOS(i,6));
end

fprintf(fileID,"};\n\n");

fprintf(fileID,"const float scaleFactors[SOS_NUM] = {\n ");

TEMP=G(1:G_num-1,1);%取G前G_num-1个数
count=0;
for i=1:1:(G_num-1)
    fprintf(fileID,'%6f,',TEMP(i,1));
    count=count+1;
    if count==3
        fprintf(fileID,'\n  ');%每三个元素后就换行
        count=0;
    end
end

fprintf(fileID,"\n};\n\n");

fprintf(fileID,"#endif\n");

fclose(fileID);